// pages/balance_transfer/balance_transfer.js
import {payKeyboardAsync} from "../../../../mycomponent/payKeyboard/PayKeyboardAsync.js";
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
var app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    balance: 0,
    info_form: {
      transfer_id: "",
      transfer_money: ""
    },
    transfer_id: "",
    memberInfo: "",
    //余额字样
    balanceLang: '余额',

    //支付键盘
    payKeyboardShow:false,
    //是否需要支付密码
    need_password:false, 
    //是否设置支付密码
    has_password:false,  
    showConfirm: false //确认转账弹窗
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    try {
      let value = wx.getStorageSync('balance');
      if (value) {
        this.setData({
          balanceLang: value
        });
      }
      var integral = wx.getStorageSync('integral');
      if(integral){
        this.setData({
          integral:integral
        })
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    this.getBalence();
  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  getBalence() {
    let that = this;
    let urlStr = app.getNetAddresss("finance.balance.member-balance");
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            balance: res.data.credit2,
            need_password:res.data.need_password,
            has_password:res.data.has_password
          });
          if(res.data.deduct_point){
            res.data.deduct_point.deduct_balance = Number(res.data.deduct_point.deduct_balance);
            res.data.deduct_point.deduct_point = Number(res.data.deduct_point.deduct_point);
            that.setData({
              deduct_point:res.data.deduct_point
            })
          }
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: that.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  payKeyboardComplete(evt){
    payKeyboardAsync.complete(evt.detail);
  },
  getPayKeyboardPassword(){
    this.setData({payKeyboardShow:true});
    return new Promise((resove,reject)=>{
      this.lockClick = false;
      payKeyboardAsync.addCompleteFn((pass)=>{
        resove(pass);
      });
    });
    // let pass = await this.getPayKeyboardPassword();
    // console.log(pass);
  },
  payKeyboardClose(){
    this.setData({payKeyboardShow:false});
  },
  async beforeConfirm() {
    //确认转账前处理
    if (parseFloat(this.data.info_form.transfer_money) > parseFloat(this.data.balance)) {
      wx.showToast({
        icon: 'none',
        title: '转让金额不可大于您的余额',
        duration: 1500
      });
      return;
    }
    if (this.data.transfer_id == undefined || this.data.transfer_id <= 0 || this.data.transfer_id.length == 0) {
      wx.showToast({
        icon: 'none',
        title: '转让id不可为空',
        duration: 1500
      });
      return;
    }
    if (this.data.info_form.transfer_money == undefined || this.data.info_form.transfer_money <= 0 || this.data.info_form.length == 0) {
      wx.showToast({
        icon: 'none',
        title: '转让金额不可低于0',
        duration: 1500
      });
      return;
    }

    if(this.data.need_password==true && this.data.has_password==false){
      let confirmFlag = await app.confirm("请先设置支付密码");
      if(confirmFlag) {
        wx.navigateTo({
          url: '/packageA/member/set_balance_password/set_balance_password'
        });
      }
      return;
    }
    this.setData({
      showConfirm: true
    });
  },
  //确认转账
  async confirm() {
    var that = this;
    if(this.lockClick) return;//防止重复点击
    this.lockClick = true;
    let urlStr = app.getNetAddresss("finance.balance.transfer");
    urlStr += '&recipient=' + that.data.transfer_id;
    urlStr += '&transfer_money=' + that.data.info_form.transfer_money;

    if(this.data.need_password==true){  //开启支付密码验证
      let pass = await this.getPayKeyboardPassword();
      urlStr += '&password=' + pass;
    }

    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          this.lockClick = false;
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500,
            success: function() {
              setTimeout(function() {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          this.lockClick = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        this.lockClick = false;
        console.log(res);
      }
    });
  },
  transferIdinp(e) {
    let val = e.detail.value;
    this.setData({
      transfer_id: val
    });
    this.getmemberInfo();
  },
  moneyinp(e) {
    let val = e.detail.value;
    this.setData({
      'info_form.transfer_money': val
    });
  },
  getmemberInfo() {
    var that = this;
    let urlStr = app.getNetAddresss("member.member.memberInfo");
    urlStr += '&uid=' + this.data.transfer_id;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            memberInfo: res.data
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  }
});
